#include<stdio.h>

int is_leap(int year);
int get_month_days(int month, int year);

int main(int argc, char *argv[])
{
	int year;
	int month;
	int i;
	int days;
	int monthdays;
	int week;

	do{
		printf("欢迎使用日历系统！\n请输入年份月份(空格分隔)：");
		scanf("%d%d", &year, &month);
	}while(!(year>=1970&&month>=1&&month<=12));

	days = 0;
	for(i = 1970;i<year;i++){
		if(is_leap(i)){
			days+=366;
		}else{
			days+=365;
		}
	}

	for(i = 1 ; i < month ; i++){
		days += get_month_days(year,i); 
	}

	days += 1;//year month 1号距离1970-1-1多少天；
	week = (days + 3) % 7;

	if(0 == week){
		week = 7;
	}

	monthdays = get_month_days(year,month);
	printf("%d年的%d月1日是周%d\n", year, month, week);
	printf("      %d月 %d\n", month, year);
	printf("日 一 二 三 四 五 六\n");
	for(i = 0; i < week;i++){
		printf("   ");
	}
	for(i = 1; i <= monthdays; i++){
		printf("%2d%c", i, (i+week)%7 ? ' ' : '\n');
	}

	printf("\n");
	return 0;
}

//判断闰年
int is_leap(int year)
{
	if((year%4==0&&year%100!=0)||(year%400==0))
		return 1;
	else
		return 0;
}

//获取月份天数
int get_month_days(int year, int month){
	int monthdays;

	if(1==month || 3==month || 5==month || 7==month  || 8==month || 10==month || 12 ==month){
		monthdays = 31;
	}else if(2==month){
		if(is_leap(year)){
			monthdays = 29;
		}else{
			monthdays = 28;
		}
	}else{
		monthdays = 30;
	}

	return monthdays;
}

